package com.baizw.cultivation.module.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baizw.cultivation.common.page.CustomPage;
import com.baizw.cultivation.common.search.CommonSearch;
import com.baizw.cultivation.module.admin.entity.ResourceEntity;
import com.baizw.cultivation.module.admin.entity.vo.ResourceTreeSelect;
import com.baizw.cultivation.module.admin.entity.vo.UserMenu;
import com.baizw.cultivation.module.admin.mapper.ResourceMapper;
import com.baizw.cultivation.module.admin.mapstruct.UserMenuMapStruct;
import com.baizw.cultivation.module.admin.service.ResourceService;
import com.baizw.cultivation.util.PermissionUtil;
import com.baizw.cultivation.util.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 资源业务接口实现
 * @Author: 剧终1204
 * @create: 2018年 03月 20日 12:16
 * @Copyright: 2018 http://www.cnns.net All rights reserved.
 * 注意：本内容仅限于深圳安络科技公司内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, ResourceEntity> implements ResourceService {

    @Resource
    private UserMenuMapStruct navMapStruct;

    /**
     * 分页查询资源信息
     *
     * @return
     */
    @Override
    public IPage<ResourceEntity> getByPage(CustomPage<ResourceEntity> customPage) {
        return page(customPage, new QueryWrapper<ResourceEntity>().eq("status", "1"));
    }

    @Override
    @Cacheable(cacheNames = {"sys_resource"}, key = "#search")
    public List<ResourceEntity> listTreeBySearch(CommonSearch search) {
        boolean notBlank = StrUtil.isNotBlank(search.getSearch());
        LambdaQueryWrapper<ResourceEntity> queryWrapper = Wrappers.<ResourceEntity>lambdaQuery()
                .like(notBlank, ResourceEntity::getName, search.getSearch())
                .or().like(notBlank, ResourceEntity::getId, search.getSearch());
        List<ResourceEntity> entityList = PermissionUtil.covertPermission(super.list(queryWrapper));
        return TreeUtil.list2TreeList(entityList);
    }

    /**
     * 查询用户权限信息
     *
     * @param userId 用户ID
     * @return
     */
    @Override
    @Cacheable(cacheNames = "sys_resource", key = "#userId")
    public List<UserMenu> listResource(Long userId) {
        List<ResourceEntity> resourceEntityList = baseMapper.listResource(userId);
        return resourceEntityList.parallelStream().map(navMapStruct::convert).collect(Collectors.toList());
    }

    @Override
    public List<UserMenu> listResourceTree() {
        List<ResourceEntity> entityList = list();
        List<UserMenu> userMenuList = entityList.parallelStream().map(navMapStruct::convert).collect(Collectors.toList());
        return TreeUtil.list2TreeList(userMenuList);
    }

    @Override
    @Cacheable(cacheNames = "MENU_SELECT")
    public List<ResourceTreeSelect> selectTree() {
        List<ResourceEntity> entityList = list();
        List<ResourceTreeSelect> userMenuList = entityList.parallelStream().map(navMapStruct::covertSelect).collect(Collectors.toList());
        return TreeUtil.list2TreeList(userMenuList);
    }

    @Override
    public <E extends IPage<ResourceEntity>> E page(E page, Wrapper<ResourceEntity> queryWrapper) {
        E resourcePage = super.page(page, queryWrapper);
        List<ResourceEntity> resourceEntityList = resourcePage.getRecords();
        return (E) PermissionUtil.covertPermission(resourceEntityList);
    }
}
